#pragma once
#include "pch.h"
#include <string.h>
#include "../FType/FreeType.h"
using namespace std;
using namespace Windows::Foundation::Collections;
namespace CnyeCPlus
{
	using namespace FType;
	namespace ExtSurface{
		public enum class LineSpacingMethod
		{
			DEFAULT,UNIFORM
		};
		public value struct CharRect
		{
			wchar_t Tchar;
			Windows::Foundation::Rect Rt;
		};
		public ref class ESurfaceImageSource sealed : Windows::UI::Xaml::Media::Imaging::SurfaceImageSource
		{
		private:
			FreeType^ _FBuilder;
			int tval;
		public:
			property FreeType^ FBuilder{
				FreeType^ get(){
					return this->_FBuilder;
				}
			}
		public:
			ESurfaceImageSource(int pixelWidth, int pixelHeight, bool isOpaque);

			void BeginDraw(Windows::Foundation::Rect updateRect);
			void BeginDraw()    { BeginDraw(Windows::Foundation::Rect(0, 0, (float)m_width, (float)m_height)); }
			void EndDraw();
			void SetDpi(float dpi);
			//
			void SetStrokeWidth(float sw){
				this->StrokeWidth=sw;
			}
			void CreateStrokeStyle(const Platform::Array<float>^ dashes);
			void FreeStrokeStyle();
			//
			void Clear(Windows::UI::Color color);
			void DrawRect(Windows::UI::Color color, Windows::Foundation::Rect rect,bool full);
			void DrawRect(Windows::UI::Color color, Windows::Foundation::Rect rect){
				this->DrawRect(color,rect,false);
			}
			//
			void CreateTextFormat(Platform::String^ fontFamilyName,float fontSize,
				int fontWeight,int fontStyle,int fontStretch, Platform::String^ localName);
			void CreateTextFormat(Platform::String^ fontFamilyName,float fontSize){
				this->CreateTextFormat(fontFamilyName,fontSize,400,0,5,nullptr);
			}
			void FreeTextFormat();
			void SetLineSpacing(LineSpacingMethod lineSpacingMethod,FLOAT lineSpacing,FLOAT baseline);
			void DrawText(Platform::String^ text,Windows::UI::Color color,Windows::Foundation::Rect rect);
			//
			void CreateFBuilder(const Platform::Array<unsigned char>^ fontbuf,int fid);
			void SetFFontSize(float fontsize);
			Windows::Foundation::Size FCharGlyphSize(wchar_t c);
			bool FCanDraw(wchar_t c){
				return this->FCharGlyphSize(c).Height>0;
			}
			void DrawFText(
				const Platform::Array<CharRect>^ rts,
				Windows::UI::Color tcolor,Windows::UI::Color bcolor);
			void DrawFText(
				const Platform::Array<CharRect>^ rts,
				Windows::UI::Color tcolor){
					this->DrawFText(rts,tcolor,Windows::UI::Colors::Transparent);
			}
			void DrawFText(wchar_t c,Windows::Foundation::Rect rect,Windows::UI::Color tcolor,Windows::UI::Color bcolor);
			void DrawFText(wchar_t c,Windows::Foundation::Rect rect,Windows::UI::Color tcolor){
				this->DrawFText(c,rect,tcolor,Windows::UI::Colors::Transparent);
			}
			//
			void ShowChar();
		private:
			float StrokeWidth;
			ID2D1StrokeStyle* StrokeStyle;
		private protected:
			void CreateDeviceIndependentResources();
			void CreateDeviceResources();
			Microsoft::WRL::ComPtr<ISurfaceImageSourceNative>   m_sisNative;
			// Direct3D device
			Microsoft::WRL::ComPtr<ID3D11Device>                m_d3dDevice;
			// Direct2D objects
			Microsoft::WRL::ComPtr<ID2D1Device>                 m_d2dDevice;
			Microsoft::WRL::ComPtr<ID2D1DeviceContext>          m_d2dContext;
			int                                                 m_width;
			int                                                 m_height;

			//
			//text reference field.
			Microsoft::WRL::ComPtr<ID2D1Factory>				m_d2dFactory;
			Microsoft::WRL::ComPtr<IDWriteFactory>				m_dwriteFactory;
			Microsoft::WRL::ComPtr<IWICImagingFactory>			m_wicFactory;
			IDWriteTextFormat*									m_textFormat;

			IMap<int,FreeType^>^								m_FType;


		};
	}
}